#!/usr/bin/python
# -*- coding: UTF-8 -*-

'''
Author: LDW0331 2061278380@qq.com
Date: 2024-03-29 09:41:04
LastEditors: LDW0331 2061278380@qq.com
LastEditTime: 2024-04-03 17:08:37
FilePath: /hrg_bee_dev_v3/src/network_module/ros_app_flask_bridge/parser/robot_parser2.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

import rospy, os
import tf
import time
import yaml
from cln_msgs.msg import IpcState
from std_msgs.msg import String
import requests
import json

class Robot_Parser2:

    def reset_ubuntu_pd(self):
        """_summary_
            从配置文件中读取序列号、机器密码
            要器密码不存在时 从云平台获取
            """
        try:
            ret_dat = {"msg": 'success', "code": 0}
            # user_params_database = rospy.get_param('user_params_dir', "/home/aoting/user_config/params/")
            # robot_sn_filepath = os.path.join(user_params_database, "robot_sn.yaml")
            robot_sn_filepath = rospy.get_param('robot_sn_yaml', "/home/aoting/user_config/params/robot_sn.yaml")
            if not os.path.exists(robot_sn_filepath):
                ret_dat["code"] = -1
                ret_dat["msg"] = "fail no robot_sn.yaml"
                return ret_dat
            with open(robot_sn_filepath, "r") as file:
                data = yaml.load(file)
            if "ubuntuPd" not in data:
                pd__ = self.getCloudRobotPd(data["robot_sn"])
                if pd__:
                    data["ubuntuPd"] = pd__
            if "ubuntuPd" not in data:
                ret_dat["code"] = -1
                ret_dat["msg"] = "no cached and cloud pd"
                return ret_dat
            pd = data["ubuntuPd"]

            rospy.loginfo("change ubuntu {}".format(pd))
            finput = os.popen("sudo passwd aoting", "w")
            finput.write("{}\n".format(pd))
            finput.write("{}\n".format(pd))
            finput.close()
            ret_dat["ubuntuPd"] = pd
            return ret_dat
        except Exception as err:
            print(err)
            ret_dat = {"msg": str(err), "code": -1}
            return ret_dat

    def getCloudRobotPd(self,sn):
        """获取云平台配置的机器密码
        """
        print("getCloudRobotPd sn {}".format(sn))
        __cloudplatform_url = rospy.get_param("cloudplatform_url","cloud.aotbot.com/api")
        update_url = "{}/loginDevice".format("http://" + __cloudplatform_url)
        __headers = {"Content-Type": "application/x-www-form-urlencoded"}
        client_data={"account":sn,"password":"1234","showUbuntuPd":99}
        ret = requests.post(url=update_url,
                            data=client_data,
                            headers=__headers,
                            timeout=10)
        cloud_data = json.loads(ret.text)
        print(cloud_data)
        if cloud_data["message"]["code"]==0:
            pd=cloud_data["data"]["ubuntuPd"]
            print("getCloudRobotPd pd {}".format(pd))
            return pd
        return None
